/*->c.mnpmisc */

/*=============================================================================

                                 The Microcom MNP Library
                                        (Microsoft C Version)

-------------------------------------------------------------------------------

               MNPMISC - Miscellaneous MNP Support Routines

==========================================================================*/

/* Header files */


#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <signal.h>
#include <ctype.h>
#include <time.h>


#include "h.os"
#include "h.wimp"
#include "h.bbc"
#include "h.flex"


#include "h.wos"
#include "h.main"



#include "h.mnpdat"

/***************************************************************************
*
*       BUFFER MANAGEMENT - INITIALIZE THE ARRAY OF STRUCTURES CONTROLLING
*                               THE BUFFERS
*
*       Synopsis:       init_blst(buff, len)
*                       struct buffer *b_struct;
*                       int len;
*                       char *buff;
*
*                       returns:
*
****************************************************************************/


void init_blst(struct BUFFER * b_struct,USIGN_16 len,USIGN_8 * buff)
{
register USIGN_16 i;

for (i=0; i < b_struct->num - 1;)
        {
        b_struct->list[i].mark = 0;
        b_struct->list[i].bptr = buff + (len * i);
        b_struct->list[i].next_b = &b_struct->list[i+1];
         i++;
        }
b_struct->list[i].mark = 0;
b_struct->list[i].bptr = buff + (len * i);
b_struct->free = b_struct->list[i].next_b = b_struct->list;

b_struct->used = 0;
b_struct->used_lst = 0;

return;
}

/***************************************************************************
*
*       BUFFER MANAGEMENT - GET POINTER TO A FREE BUFFER
*
*       Synopsis:
*
*
*                       returns: SUCCESS
*                                FAILURE - no free buffer
*
***************************************************************************/

SIGN_16 get_b(struct BUFFER * b_struct,struct BLST ** bl_struct)
{
if ((*bl_struct = b_struct->free)!=NULL)
        {
        (*bl_struct)->mark = 1;
        if (++b_struct->used == b_struct->num)
                b_struct->free = 0;
        else
                b_struct->free = b_struct->free->next_b;
        return (SUCCESS);
        }
else
        return (FAILURE);
}

/***************************************************************************
*
*       BUFFER MANAGEMENT - RETURN A BUFFER TO THE FREE POOL
*
*       Synopsis:
*
*
*
*                       returns: 0: success
*
****************************************************************************/


SIGN_16 ret_b(struct BUFFER *b_struct,struct BLST *bl_struct)
{
if (bl_struct->mark)
        {
        bl_struct->mark = 0;
        b_struct->used--;
        if (b_struct->free == NULL)
                b_struct->free = bl_struct;
        }
return (SUCCESS);
}



/***************************************************************************
*
*       BUFFER MANAGEMENT - RESET THE WHOLE BUFFER POOL TO BE FREE
*
*       Synopsis:
*
*
*
****************************************************************************/

void reset_blst(struct BUFFER *b_struct)
{
register USIGN_16 i;

for (i=0; i < b_struct->num; i++)
        b_struct->list[i].mark = b_struct->list[i].len = 0;  /* mark all as free */

b_struct->used = 0;
b_struct->used_lst = 0;
b_struct->free = b_struct->list;
}


/***************************************************************************
*
*
*
*
****************************************************************************/

USIGN_16 min (USIGN_16 a,USIGN_16  b)
{
return ((a) < (b) ? (a): (b));
}

USIGN_16 max (USIGN_16 a,USIGN_16 b)
{
return ((a) > (b) ? (a): (b));
}
